
-- number of subscribers (listeners) for this server
_subscribers = 0

function onConnection(client, ...)
    INFO("Connection of a new client on ", mona.configs["host"])
    
    function client:onPublish(publication)
        INFO("client onPublish:", publication.fullName)
        -- informs the following server about this publication
        for id, following in pairs(mona.servers) do
            INFO("send publish to ",following.address," ",publication.fullName)
            following:send("publish", publication.fullName)
        end
        function publication:onVideo(time, packet, type)
            if type > 0 then 
                publication.videoCodec = packet
                INFO("got videoCodec")
             end
            for id, following in pairs(mona.servers) do
                if following.isConnected and not following.videoCodecs[publication.fullName] then
                    following.videoCodecs[publication.fullName] = true
                    INFO("following send videoCodec:", id, publication.fullName)
                    following:send("video", publication.fullName, 0, publication.videoCodec)
                end
                -- forward the video packet to the following server
                following:send("video", publication.fullName, time, packet)
            end
        end
        function publication:onAudio(time, packet, type)
            if type > 0 then publication.audioCodec = packet end
            for id, following in pairs(mona.servers) do
                if following.isConnected and not following.audioCodecs[publication.fullName] then
                    following.audioCodecs[publication.fullName] = true
                    INFO("following send audioCodec:", id, publication.fullName)
                    following:send("audio", publication.fullName, 0, publication.audioCodec)
                end
                -- forward the audio packet to the following server
                following:send("audio", publication.fullName, time, packet)
            end
        end
        function publication:onData(name, packet)
            INFO("onData : ", name, " - ", packet)
            for id, following in pairs(mona.servers) do
                -- forward the data packet to the following server
                following:send("data", publication.fullName, name, packet)
            end
        end
    end
    
    function client:onUnpublish(publication)
        -- informs the following server about this unpublication
        for id, following in pairs(mona.servers) do
            following.videoCodecs = {}
            following.audioCodecs = {}
            following:send("unpublish", publication.fullName)
        end
    end
    
    function client:onSubscribe(listener)
        -- if a following server exist, and if this server has more than 400 subscribers
        -- redirect the client to the following server:
        -- I send an error with the redirection server address in its description
        INFO("Subscription of client ", client.address, " (_subscribers=", _subscribers, ")")
        --if _nextServer and _subscribers>=400 then error(_nextServer.host) end
        _subscribers = _subscribers + 1
    end
    
    function client:onUnsubscribe(listener)
        _subscribers = _subscribers - 1
    end
	
end

function onServerConnection(server)
    -- informs the following server about my publications
    INFO("server connected:",server.address)
	function server:onConnected()
		for id, publication in pairs(mona.publications) do
			INFO("send publish to server",server.address," ", publication.fullName)
            server.videoCodecs = {}
            server.audioCodecs = {}
		 server:send("publish", publication.fullName)
		end
        server.isConnected = true
	end
end

function onServerDisconnection(server)
    -- disconnected server was a following server!
  
end
